define(function(require, exports, module) {
    "require:nomunge,exports:nomunge,module:nomunge";
    "use strict";

    var view = require('mokit/view');
    var utils = require('mokit/utils');
    var app = require('mokit/app');
    var $event = require('mokit/event');
    var $ = require('mokit/jquery');
    var move = require('mokit/move');

    require('./style.css');

    /**
     * 手势解锁控制
     */
    return view.create({
        template: module.resovleUri('./tmpl.html'),
        el: {
            sliderState: '.slider-state',
            sliderMask: '.slider-mask'
        },

        onInit: function(context) {
            var self = this;
            //启用事件
            $event.use(self);
        },

        /**
         * 在视图呈现时
         */
        onRender: function(context) {
            var self = this;
            self.options = self.options || {};
            utils.async(function() {
                self.options.duration = self.options.duration || 360;
                self.width = self.width || self.ui.width();
            }, 600);
        },

        /**
         * 在移除时
         */
        onRemove: function(context) {
            var self = this;
            self.off();
            if (self.ui && self.ui[0] && self.ui[0].off) {
                self.ui[0].off();
            }
        },

        /**
         * 设置显示的项
         */
        setItems: function(context, items) {
            var self = this;
            self.items = items || [];
            self.currentIndex = 0;
            self.currentSlider = null;
            self.nextSlider = null;
            self.switchSlider(context, 'center');
        },

        next: function(context) {
            var self = this;
            self.currentIndex++;
            self.checkIndex(context);
            self.switchSlider(context, 'left');
        },

        pre: function(context) {
            var self = this;
            self.currentIndex--;
            self.checkIndex(context);
            self.switchSlider(context, 'right');
        },

        go: function(context, index) {
            var self = this;
            self.currentIndex = index;
            self.checkIndex(context);
            self.switchSlider(context, 'center');
        },

        checkIndex: function(context) {
            var self = this;
            if (self.currentIndex > self.items.length - 1) {
                self.currentIndex = 0;
            }
            if (self.currentIndex < 0) {
                self.currentIndex = self.items.length - 1;
            }
        },

        maskSwipe: function(context) {
            return false;
        },

        switchSlider: function(context, dir) {
            var self = this;
            self.el.sliderMask.show();
            self.currentItem = self.items[self.currentIndex];
            if (self.currentItem == null) return;
            //第一次呈现
            if (self.currentSlider == null) {
                self.currentSlider = $("<div class='slider-item' style='background:url(" + self.currentItem.image + ");'></div>");
                self.ui.append(self.currentSlider);
                self.switchSliderDone(context);
                return;
            }
            //创建下一个
            self.nextSlider = $("<div class='slider-item' style='background:url(" + self.currentItem.image + ");'></div>");
            self.ui.append(self.nextSlider);

            //滑动呈现
            if (dir == 'left') {
                move(self.nextSlider[0])
                    .duration(0)
                    .x(self.width)
                    .end();
                move(self.currentSlider[0])
                    .duration(self.options.duration)
                    .ease('ease-in')
                    .x(-self.width)
                    .end();
                move(self.nextSlider[0])
                    .duration(self.options.duration)
                    .ease('ease-in')
                    .x(0)
                    .end(function() {
                        self.switchSliderDone(context);
                    });
            } else if (dir == 'right') {
                move(self.nextSlider[0])
                    .duration(0)
                    .x(-self.width)
                    .end();
                move(self.currentSlider[0])
                    .duration(self.options.duration)
                    .ease('ease-in')
                    .x(self.width)
                    .end();
                move(self.nextSlider[0])
                    .duration(self.options.duration)
                    .ease('ease-in')
                    .x(0)
                    .end(function() {
                        self.switchSliderDone(context);
                    });
            } else {
                //NONE
            }
        },

        updateState: function(context) {
            var self = this;
            var buffer = [];
            utils.each(self.items, function(i, item) {
                buffer.push('<span class="slider-state-item ' + (i == self.currentIndex ? 'slider-state-item-current' : '') + '">●</span>');
            });
            self.el.sliderState.html(buffer.join(' '));
        },

        switchSliderDone: function(context) {
            var self = this;
            //清理
            if (self.currentSlider != null && self.nextSlider != null) {
                self.currentSlider.remove();
                self.currentSlider = self.nextSlider;
                self.nextSlider = null;
            }
            self.updateState(context);
            self.call('switch', self.getEventArgs(context));
            self.el.sliderMask.hide();
        },

        getEventArgs: function(context) {
            var self = this;
            return {
                item: self.currentItem,
                index: self.currentIndex,
                slider: self.currentSlider
            }
        },

        /**
         * 在单击时
         */
        onTap: function(context) {
            var self = this
            self.call('tap', self.getEventArgs(context));
        }
    });
});